﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class ManualToolMnt : Form
    {
        string 手工具編號, 手工具名稱, 規格型號, 材料編號, 廠牌代號, 請購人員代號,
               工具類別代號, 存放地點代號, 數量, 備註, 數量_2;
        object 照片;
        string str=null;
        bool b手工具Search, b手工具Insert, b手工具Edit, b手工具Del;
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        public ManualToolMnt(bool b手工具Search, bool b手工具Insert, bool b手工具Edit, bool b手工具Del)
        {
            InitializeComponent();
            this.b手工具Del = b手工具Del;
            this.b手工具Edit = b手工具Edit;
            this.b手工具Insert = b手工具Insert;
            this.b手工具Search = b手工具Search;
        }

        private void bt_新增_Click(object sender, EventArgs e)
        {
            if (!b手工具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                ManualToolMnt_Insert frm = new ManualToolMnt_Insert(1, "", "", "", "", "", "", "", "", "", "", "");
                frm.ShowDialog();
                Load_手工具台帳();
            }
        }

        private void bt_修改_Click(object sender, EventArgs e)
        {
            if (!b手工具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                ManualToolMnt_Insert frm = new ManualToolMnt_Insert(2, 手工具編號, 手工具名稱, 規格型號, 材料編號, 廠牌代號, 請購人員代號,
               工具類別代號, 存放地點代號, 照片, 數量, 備註);
                frm.ShowDialog();
                Load_手工具台帳();
            }
        }
        public void Load_手工具台帳()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_手工具.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具);
                dataGrid_手工具.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                cb_狀態.Text = "在庫";
                //txt_名稱.Text = "";
                //txt_備註.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void bt_搜尋_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp;
                sp = "sp_手工具_search";
                if (cb_狀態.Text.Equals("領用"))
                    sp = "sp_手工具_search_領用";
                if (cb_狀態.Text.Equals("借用"))
                    sp = "sp_手工具_search_借用";
                if (cb_狀態.Text.Equals("送檢"))
                    sp = "sp_手工具_search_送檢";
                if (cb_狀態.Text.Equals("維修"))
                    sp = "sp_手工具_search_維修";
                if (cb_狀態.Text.Equals("報廢"))
                    sp = "sp_手工具_search_報廢";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                if (txt_手工具編號.Text != "") cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                if (txt_手工具名稱.Text != "") cmd0.Parameters.AddWithValue("@手工具名稱", txt_手工具名稱.Text.ToString());
                if (txt_工具類別.Text != "") cmd0.Parameters.AddWithValue("@工具類別", txt_工具類別.Text.ToString());
                if (txt_材料編號.Text != "") cmd0.Parameters.AddWithValue("@材料編號", txt_材料編號.Text.ToString());
                if (txt_規格型號.Text != "") cmd0.Parameters.AddWithValue("@規格型號", txt_規格型號.Text.ToString());
                if (txt_存放地點.Text != "") cmd0.Parameters.AddWithValue("@存放地點", txt_存放地點.Text.ToString());
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();
                //***************
                if (cb_狀態.Text.Equals("在庫"))
                {
                    dt.Columns.Add("手工具編號");
                    dt.Columns.Add("手工具名稱");
                    dt.Columns.Add("規格型號");
                    dt.Columns.Add("在庫數量");
                    dt.Columns.Add("材料編號");
                    dt.Columns.Add("工具類別");
                    dt.Columns.Add("存放地點");
                    dt.Columns.Add("備註");

                    while (sqlreader.Read())
                    {
                        DataRow dtrows = dt.NewRow();
                        dtrows["手工具編號"] = sqlreader["手工具編號"];
                        dtrows["手工具名稱"] = sqlreader["手工具名稱"];
                        dtrows["規格型號"] = sqlreader["規格型號"];
                        dtrows["在庫數量"] = sqlreader["數量"];
                        dtrows["材料編號"] = sqlreader["材料編號"];
                        dtrows["工具類別"] = sqlreader["工具類別"];
                        dtrows["存放地點"] = sqlreader["存放地點"];
                        dtrows["備註"] = sqlreader["備註"];
                        dt.Rows.Add(dtrows);
                    }
                }
                if (cb_狀態.Text.Equals("領用"))
                {
                    dt.Columns.Add("手工具編號");
                    dt.Columns.Add("手工具名稱");
                    dt.Columns.Add("規格型號");
                    dt.Columns.Add("領用部門");
                    dt.Columns.Add("領用人");
                    dt.Columns.Add("受理人");
                    dt.Columns.Add("領用數量");
                    dt.Columns.Add("領用日期");
                    dt.Columns.Add("歸還日期");
                    dt.Columns.Add("備註");

                    while (sqlreader.Read())
                    {
                        DataRow dtrows = dt.NewRow();
                        //dtrows["項次"] = sqlreader["項次"];
                        dtrows["手工具編號"] = sqlreader["手工具編號"];
                        dtrows["手工具名稱"] = sqlreader["手工具名稱"];
                        dtrows["規格型號"] = sqlreader["規格型號"];
                        dtrows["領用部門"] = sqlreader["領用部門"];
                        dtrows["領用人"] = sqlreader["領用人"];
                        dtrows["受理人"] = sqlreader["受理人"];
                        dtrows["領用數量"] = sqlreader["數量"];
                        dtrows["領用日期"] = sqlreader["領用日期"];
                        dtrows["歸還日期"] = sqlreader["歸還日期"];
                        dtrows["備註"] = sqlreader["備註"];
                        dt.Rows.Add(dtrows);
                    }
                }
                if (cb_狀態.Text.Equals("借用"))
                {
                    dt.Columns.Add("手工具編號");
                    dt.Columns.Add("手工具名稱");
                    dt.Columns.Add("規格型號");
                    dt.Columns.Add("借用部門");
                    dt.Columns.Add("借用人");
                    dt.Columns.Add("受理人");
                    dt.Columns.Add("借用數量");
                    dt.Columns.Add("借用日期");
                    dt.Columns.Add("預還歸還日期");
                    dt.Columns.Add("備註");

                    while (sqlreader.Read())
                    {
                        DataRow dtrows = dt.NewRow();
                        //dtrows["項次"] = sqlreader["項次"];
                        dtrows["手工具編號"] = sqlreader["手工具編號"];
                        dtrows["手工具名稱"] = sqlreader["手工具名稱"];
                        dtrows["規格型號"] = sqlreader["規格型號"];
                        dtrows["借用部門"] = sqlreader["借用部門"];
                        dtrows["借用人"] = sqlreader["借用人"];
                        dtrows["受理人"] = sqlreader["受理人"];
                        dtrows["借用數量"] = sqlreader["數量"];
                        dtrows["借用日期"] = sqlreader["借用日期"];
                        dtrows["預還歸還日期"] = sqlreader["預還歸還日期"];
                        dtrows["備註"] = sqlreader["備註"];
                        dt.Rows.Add(dtrows);
                    }
                }
                if (cb_狀態.Text.Equals("送檢"))
                {
                    dt.Columns.Add("手工具編號");
                    dt.Columns.Add("手工具名稱");
                    dt.Columns.Add("規格型號");
                    dt.Columns.Add("受理人");
                    dt.Columns.Add("送檢數量");
                    dt.Columns.Add("送檢日期");
                    dt.Columns.Add("預定歸還日期");
                    dt.Columns.Add("備註");

                    while (sqlreader.Read())
                    {
                        DataRow dtrows = dt.NewRow();
                        //dtrows["項次"] = sqlreader["項次"];
                        dtrows["手工具編號"] = sqlreader["手工具編號"];
                        dtrows["手工具名稱"] = sqlreader["手工具名稱"];
                        dtrows["規格型號"] = sqlreader["規格型號"];
                        dtrows["受理人"] = sqlreader["受理人"];
                        dtrows["送檢數量"] = sqlreader["數量"];
                        dtrows["送檢日期"] = sqlreader["送檢日期"];
                        dtrows["預定歸還日期"] = sqlreader["預定歸還日期"];
                        dtrows["備註"] = sqlreader["備註"];
                        dt.Rows.Add(dtrows);
                    }
                }
                if (cb_狀態.Text.Equals("維修"))
                {
                    dt.Columns.Add("手工具編號");
                    dt.Columns.Add("手工具名稱");
                    dt.Columns.Add("規格型號");
                    dt.Columns.Add("受理人");
                    dt.Columns.Add("維修數量");
                    dt.Columns.Add("維修日期");
                    dt.Columns.Add("預定歸還日期");
                    dt.Columns.Add("備註");

                    while (sqlreader.Read())
                    {
                        DataRow dtrows = dt.NewRow();
                        //dtrows["項次"] = sqlreader["項次"];
                        dtrows["手工具編號"] = sqlreader["手工具編號"];
                        dtrows["手工具名稱"] = sqlreader["手工具名稱"];
                        dtrows["規格型號"] = sqlreader["規格型號"];
                        dtrows["受理人"] = sqlreader["受理人"];
                        dtrows["維修數量"] = sqlreader["數量"];
                        dtrows["維修日期"] = sqlreader["維修日期"];
                        dtrows["預定歸還日期"] = sqlreader["預定歸還日期"];
                        dtrows["備註"] = sqlreader["備註"];
                        dt.Rows.Add(dtrows);
                    }
                }
                if (cb_狀態.Text.Equals("報廢"))
                {
                    dt.Columns.Add("手工具編號");
                    dt.Columns.Add("手工具名稱");
                    dt.Columns.Add("規格型號");
                    dt.Columns.Add("受理人");
                    dt.Columns.Add("報廢數量");
                    dt.Columns.Add("報廢日期");
                    dt.Columns.Add("預定歸還日期");
                    dt.Columns.Add("備註");

                    while (sqlreader.Read())
                    {
                        DataRow dtrows = dt.NewRow();
                        //dtrows["項次"] = sqlreader["項次"];
                        dtrows["手工具編號"] = sqlreader["手工具編號"];
                        dtrows["手工具名稱"] = sqlreader["手工具名稱"];
                        dtrows["規格型號"] = sqlreader["規格型號"];
                        dtrows["受理人"] = sqlreader["受理人"];
                        dtrows["報廢數量"] = sqlreader["數量"];
                        dtrows["報廢日期"] = sqlreader["報廢日期"];
                        dtrows["預定歸還日期"] = sqlreader["預定歸還日期"];
                        dtrows["備註"] = sqlreader["備註"];
                        dt.Rows.Add(dtrows);
                    }
                }

                dataGrid_手工具.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //con.Close();
            }
        }

        private void bt_重置_Click(object sender, EventArgs e)
        {
            txt_存放地點.Text = "";
            txt_工具類別.Text = "";
            txt_手工具名稱.Text = "";
            txt_手工具編號.Text = "";
            txt_材料編號.Text = "";
            txt_規格型號.Text = "";
            Load_手工具台帳();
            cb_狀態.Text = "在庫";
        }

        private void bt_手工具存放地點_存檔_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_手工具存放地點_search";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                if (txt_存放地點代號.Text != "") cmd0.Parameters.AddWithValue("@存放地點代號", txt_存放地點代號.Text.ToString());
                if (txt_存放地點名稱.Text != "") cmd0.Parameters.AddWithValue("@存放地點名稱", txt_存放地點名稱.Text.ToString());
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();

                dt.Columns.Add("存放地點代號");
                dt.Columns.Add("存放地點名稱");

                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["存放地點代號"] = sqlreader["存放地點代號"];
                    dtrows["存放地點名稱"] = sqlreader["存放地點名稱"];
                    dt.Rows.Add(dtrows);
                }

                dataGrid_手工具存放地點.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具存放地點);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //con.Close();
            }
        }

        private void bt_手工具存放地點_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_存放地點代號.Text.ToString() == null || txt_存放地點代號.Text.ToString().Equals(""))
                    MessageBox.Show("存放地點代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具存放地點 WHERE 存放地點代號='" + txt_存放地點代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_存放地點代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_手工具存放地點_insert", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@存放地點代號", txt_存放地點代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@存放地點名稱", txt_存放地點名稱.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_手工具存放地點();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具存放地點_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_存放地點代號.Text.ToString() == null || txt_存放地點代號.Text.ToString().Equals(""))
                    MessageBox.Show("存放地點代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具存放地點 WHERE 存放地點代號='" + str + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具存放地點_update", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@存放地點代號_1", str);
                        cmd0.Parameters.AddWithValue("@存放地點代號_2", txt_存放地點代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@存放地點名稱", txt_存放地點名稱.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        Load_手工具存放地點();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_存放地點代號.Text.ToString() + "' 不由來");                       
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具存放地點_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_存放地點代號.Text.ToString() == null || txt_存放地點代號.Text.ToString().Equals(""))
                    MessageBox.Show("存放地點代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具存放地點 WHERE 存放地點代號='" + txt_存放地點代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具存放地點_delete", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@存放地點代號", txt_存放地點代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        Load_手工具存放地點();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_存放地點代號.Text.ToString() + "' 不由來");
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具存放地點_取消_Click(object sender, EventArgs e)
        {
            txt_存放地點代號.Text = "";
            txt_存放地點名稱.Text = "";
            Load_手工具存放地點();
        }

        public void Load_手工具存放地點()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具存放地點_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_手工具存放地點.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具存放地點);
                dataGrid_手工具存放地點.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                txt_存放地點代號.Text = "";
                txt_存放地點名稱.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具廠牌()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具廠牌_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_手工具廠牌.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具廠牌);
                dataGrid_手工具廠牌.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                txt_廠牌代號.Text = "";
                txt_廠牌名稱.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具類別()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具類別_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_手工具類別.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具類別);
                dataGrid_手工具類別.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                txt_手工具類別代號.Text = "";
                txt_手工具類別名稱.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_請購人員()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_請購人員_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_請購人員.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_請購人員);
                dataGrid_請購人員.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                txt_請購人員代號.Text = "";
                txt_請購人員姓名.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void bt_手工具廠牌_存檔_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_手工具廠牌_search";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                if (txt_廠牌代號.Text != "") cmd0.Parameters.AddWithValue("@廠牌代號", txt_廠牌代號.Text.ToString());
                if (txt_廠牌名稱.Text != "") cmd0.Parameters.AddWithValue("@廠牌名稱", txt_廠牌名稱.Text.ToString());
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();

                dt.Columns.Add("廠牌代號");
                dt.Columns.Add("廠牌名稱");

                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["廠牌代號"] = sqlreader["廠牌代號"];
                    dtrows["廠牌名稱"] = sqlreader["廠牌名稱"];
                    dt.Rows.Add(dtrows);
                }

                dataGrid_手工具廠牌.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具廠牌);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_手工具廠牌_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_廠牌代號.Text.ToString() == null || txt_廠牌代號.Text.ToString().Equals(""))
                    MessageBox.Show("廠牌代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具廠牌 WHERE 廠牌代號='" + txt_廠牌代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_廠牌代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_手工具廠牌_insert", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@廠牌代號", txt_廠牌代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@廠牌名稱", txt_廠牌名稱.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_手工具廠牌();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具廠牌_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_廠牌代號.Text.ToString() == null || txt_廠牌代號.Text.ToString().Equals(""))
                    MessageBox.Show("廠牌代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具廠牌 WHERE 廠牌代號='" + str + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具廠牌_update", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@廠牌代號_1", str);
                        cmd0.Parameters.AddWithValue("@廠牌代號_2", txt_廠牌代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@廠牌名稱", txt_廠牌名稱.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        Load_手工具廠牌();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_廠牌代號.Text.ToString() + "' 不由來");                        
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具廠牌_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_廠牌代號.Text.ToString() == null || txt_廠牌代號.Text.ToString().Equals(""))
                    MessageBox.Show("廠牌代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具廠牌 WHERE 廠牌代號='" + txt_廠牌代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具廠牌_delete", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@廠牌代號", txt_廠牌代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        Load_手工具廠牌();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_廠牌代號.Text.ToString() + "' 不由來");
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具廠牌_取消_Click(object sender, EventArgs e)
        {
            txt_廠牌代號.Text = "";
            txt_廠牌名稱.Text = "";
            Load_手工具廠牌();
        }

        private void bt_請購人員_存檔_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_請購人員_search";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                if (txt_請購人員代號.Text != "") cmd0.Parameters.AddWithValue("@請購人員代號", txt_請購人員代號.Text.ToString());
                if (txt_請購人員姓名.Text != "") cmd0.Parameters.AddWithValue("@請購人員姓名", txt_請購人員姓名.Text.ToString());
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();

                dt.Columns.Add("請購人員代號");
                dt.Columns.Add("請購人員姓名");

                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["請購人員代號"] = sqlreader["請購人員代號"];
                    dtrows["請購人員姓名"] = sqlreader["姓名"];
                    dt.Rows.Add(dtrows);
                }

                dataGrid_請購人員.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_請購人員);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_請購人員_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_請購人員代號.Text.ToString() == null || txt_請購人員代號.Text.ToString().Equals(""))
                    MessageBox.Show("請購人員代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具請購人員 WHERE 請購人員代號='" + txt_請購人員代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_請購人員代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_請購人員_insert", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@請購人員代號", txt_請購人員代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@請購人員姓名", txt_請購人員姓名.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_請購人員();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_請購人員_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_請購人員代號.Text.ToString() == null || txt_請購人員代號.Text.ToString().Equals(""))
                    MessageBox.Show("請購人員代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具請購人員 WHERE 請購人員代號='" + str + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_請購人員_update", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@請購人員代號_1", str);
                        cmd0.Parameters.AddWithValue("@請購人員代號_2", txt_請購人員代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@請購人員姓名", txt_請購人員姓名.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        Load_請購人員();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_請購人員代號.Text.ToString() + "' 不由來");
                        
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_請購人員_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_請購人員代號.Text.ToString() == null || txt_請購人員代號.Text.ToString().Equals(""))
                    MessageBox.Show("請購人員代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具請購人員 WHERE 請購人員代號='" + txt_請購人員代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_請購人員_delete", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@請購人員代號", txt_請購人員代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        Load_請購人員();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_請購人員代號.Text.ToString() + "' 不由來");

                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_請購人員_取消_Click(object sender, EventArgs e)
        {
            txt_請購人員代號.Text = "";
            txt_請購人員姓名.Text = "";
            Load_請購人員();
        }

        private void bt_手工具類別_存檔_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_手工具類別_search";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                if (txt_手工具類別代號.Text != "") cmd0.Parameters.AddWithValue("@手工具類別代號", txt_手工具類別代號.Text.ToString());
                if (txt_手工具類別名稱.Text != "") cmd0.Parameters.AddWithValue("@手工具類別名稱", txt_手工具類別名稱.Text.ToString());
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();

                dt.Columns.Add("手工具類別代號");
                dt.Columns.Add("手工具類別名稱");

                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["手工具類別代號"] = sqlreader["手工具類別代號"];
                    dtrows["手工具類別名稱"] = sqlreader["手工具類別名稱"];
                    dt.Rows.Add(dtrows);
                }

                dataGrid_手工具類別.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具類別);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_手工具類別_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_手工具類別代號.Text.ToString() == null || txt_手工具類別代號.Text.ToString().Equals(""))
                    MessageBox.Show("手工具類別代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具類別 WHERE 手工具類別代號='" + txt_手工具類別代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_手工具類別代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_手工具類別_insert", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@手工具類別代號", txt_手工具類別代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@手工具類別名稱", txt_手工具類別名稱.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_手工具類別();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具類別_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_手工具類別代號.Text.ToString() == null || txt_手工具類別代號.Text.ToString().Equals(""))
                    MessageBox.Show("手工具類別代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具類別 WHERE 手工具類別代號='" + str + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具類別_update", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@手工具類別代號_1", str);
                        cmd0.Parameters.AddWithValue("@手工具類別代號_2", txt_手工具類別代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@手工具類別名稱", txt_手工具類別名稱.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        Load_手工具類別();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_手工具類別代號.Text.ToString() + "' 不由來");
                        
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具類別_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_手工具類別代號.Text.ToString() == null || txt_手工具類別代號.Text.ToString().Equals(""))
                    MessageBox.Show("手工具類別代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具類別 WHERE 手工具類別代號='" + txt_手工具類別代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具類別_delete", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@手工具類別代號", txt_手工具類別代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        Load_手工具類別();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_手工具類別代號.Text.ToString() + "' 不由來");
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt__手工具類別_取消_Click(object sender, EventArgs e)
        {
            txt_手工具類別代號.Text = "";
            txt_手工具類別名稱.Text = "";
            Load_手工具類別();
        }

        private void dataGrid_手工具存放地點_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_手工具存放地點.Rows[e.RowIndex];
                str = row.Cells[0].Value.ToString();
                txt_存放地點代號.Text = row.Cells[0].Value.ToString();
                txt_存放地點名稱.Text = row.Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_手工具廠牌_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_手工具廠牌.Rows[e.RowIndex];
                str = row.Cells[0].Value.ToString();
                txt_廠牌代號.Text = row.Cells[0].Value.ToString();
                txt_廠牌名稱.Text = row.Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_請購人員_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_請購人員.Rows[e.RowIndex];
                str = row.Cells[0].Value.ToString();
                txt_請購人員代號.Text = row.Cells[0].Value.ToString();
                txt_請購人員姓名.Text = row.Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_手工具類別_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_手工具類別.Rows[e.RowIndex];
                str = row.Cells[0].Value.ToString();
                txt_手工具類別代號.Text = row.Cells[0].Value.ToString();
                txt_手工具類別名稱.Text = row.Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void ManualToolMnt_Load_1(object sender, EventArgs e)
        {
            Load_手工具台帳();
            if (!b手工具Insert)
            {
                bt_新增.Enabled = false;
                bt_手工具存放地點_新增.Enabled = false;
                bt_手工具廠牌_新增.Enabled = false;
                bt_請購人員_新增.Enabled = false;
                bt_手工具類別_新增.Enabled = false;
            }
            if (!b手工具Edit)
            {
                bt_手工具類別_修改.Enabled = false;
                bt_請購人員_修改.Enabled = false;
                bt_手工具廠牌_修改.Enabled = false;
                bt_手工具存放地點_修改.Enabled = false;
                bt_修改.Enabled = false;
                bt_借用.Enabled = false;
                bt_報廢.Enabled = false;
                bt_維修.Enabled = false;
                bt_送檢.Enabled = false;
                bt_領用.Enabled = false;
            }
            if(!b手工具Del){
                bt_刪除.Enabled = false;
                bt_手工具存放地點_刪除.Enabled = false;
                bt_手工具廠牌_刪除.Enabled = false;
                bt_請購人員_刪除.Enabled = false;
                bt_手工具類別_刪除.Enabled = false;
            }
            if(!b手工具Search){
                bt_手工具類別_存檔.Enabled = false;
                bt_請購人員_存檔.Enabled = false;
                bt_手工具廠牌_存檔.Enabled = false;
                bt_手工具存放地點_存檔.Enabled = false;
                bt_搜尋.Enabled = false;
            }
        }

        private void tabControl_手工具_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl_手工具.SelectedIndex == 0)
            {
                Load_手工具台帳();
                PublicClass.setRowNumber(dataGrid_手工具);
            }
            if (tabControl_手工具.SelectedIndex == 1)
            {
                Load_手工具存放地點();
                PublicClass.setRowNumber(dataGrid_手工具存放地點);
            }
            if (tabControl_手工具.SelectedIndex == 2)
            {
                Load_手工具廠牌();
                PublicClass.setRowNumber(dataGrid_手工具廠牌);
            }
            if (tabControl_手工具.SelectedIndex == 3)
            {
                Load_請購人員();
                PublicClass.setRowNumber(dataGrid_請購人員);
            }
            if (tabControl_手工具.SelectedIndex == 4)
            {
                Load_手工具類別();
                PublicClass.setRowNumber(dataGrid_手工具類別);
            }
        }

        private void dataGrid_手工具_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_手工具_gridView";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_手工具.Rows[e.RowIndex];
                cmd0.Parameters.AddWithValue("@手工具編號", row.Cells[0].Value.ToString()); 
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                while (sqlreader.Read())
                {
                    手工具編號 = sqlreader["手工具編號"].ToString();
                    手工具名稱 = sqlreader["手工具名稱"].ToString();
                    規格型號 = sqlreader["規格型號"].ToString();
                    材料編號 = sqlreader["材料編號"].ToString();
                    廠牌代號 = sqlreader["廠牌代號"].ToString();
                    請購人員代號 = sqlreader["請購人員代號"].ToString();
                    工具類別代號 = sqlreader["工具類別代號"].ToString();
                    存放地點代號 = sqlreader["存放地點代號"].ToString();
                    //手工具狀態 = sqlreader["手工具狀態"].ToString();
                    照片 = sqlreader["照片"];
                    數量 = sqlreader["數量"].ToString();
                    備註 = sqlreader["備註"].ToString();
                }
                con.Close();
                SqlConnection con1 = new SqlConnection(connectionString);
                con1.Open();
                string sql;
                int lingyong = 0, zaiku = 0, jiayong = 0, songjian = 0, weixiu = 0, baofei = 0;
                sql = "SELECT 數量 FROM 手工具 WHERE 手工具編號='" + 手工具編號 + "'";
                SqlCommand cmd;
                cmd = new SqlCommand(sql, con1);
                if (cmd.ExecuteScalar()!=null)
                {
                    zaiku = Int32.Parse(cmd.ExecuteScalar().ToString());
                }
                txt_在庫數量.Text = zaiku.ToString();
                數量_2 = zaiku.ToString();
                sql = "SELECT COUNT(*) FROM 手工具領用 WHERE 手工具編號='" + 手工具編號 + "'";
                cmd = new SqlCommand(sql, con1);
                if ((int)cmd.ExecuteScalar() > 0)
                {
                    sql = "SELECT SUM(數量) FROM 手工具領用 WHERE 手工具編號='" + 手工具編號 + "'";
                    cmd = new SqlCommand(sql, con1);
                    lingyong = Int32.Parse(cmd.ExecuteScalar().ToString());
                }
                //
                sql = "SELECT COUNT(*) FROM 手工其維修 WHERE 手工具編號='" + 手工具編號 + "'";
                cmd = new SqlCommand(sql, con1);
                if ((int)cmd.ExecuteScalar() > 0)
                {
                    sql = "SELECT SUM(數量) FROM 手工其維修 WHERE 手工具編號='" + 手工具編號 + "'";
                    cmd = new SqlCommand(sql, con1);
                    weixiu = Int32.Parse(cmd.ExecuteScalar().ToString());
                }
                //
                sql = "SELECT COUNT(*) FROM 手工具送檢 WHERE 手工具編號='" + 手工具編號 + "'";
                cmd = new SqlCommand(sql, con1);
                if ((int)cmd.ExecuteScalar() > 0)
                {
                    sql = "SELECT SUM(數量) FROM 手工具送檢 WHERE 手工具編號='" + 手工具編號 + "'";
                    cmd = new SqlCommand(sql, con1);
                    songjian = Int32.Parse(cmd.ExecuteScalar().ToString());
                }
                //
                sql = "SELECT COUNT(*) FROM 手工具報廢 WHERE 手工具編號='" + 手工具編號 + "'";
                cmd = new SqlCommand(sql, con1);
                if ((int)cmd.ExecuteScalar() > 0)
                {
                    sql = "SELECT SUM(數量) FROM 手工具報廢 WHERE 手工具編號='" + 手工具編號 + "'";
                    cmd = new SqlCommand(sql, con1);
                    baofei = Int32.Parse(cmd.ExecuteScalar().ToString());
                }
                //**********************
                txt_入庫數量.Text = (lingyong + zaiku + jiayong + weixiu + songjian + baofei).ToString();
                con1.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_刪除_Click(object sender, EventArgs e)
        {
            if (!b手工具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具 WHERE 手工具編號='" + 手工具編號 + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        DialogResult answer = MessageBox.Show("您確定要刪除資料 ?", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            cmd0 = new SqlCommand("sp_手工具_delete", con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@手工具編號", 手工具編號);
                            cmd0.ExecuteNonQuery();
                            MessageBox.Show("刪除成功");
                            Load_手工具台帳();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Error! 資料不由來");
                    }
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }         
        }

        private void bt_離開_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_領用_Click(object sender, EventArgs e)
        {
            str = "領用";
            ManualTool_LingYong_Insert frm = new ManualTool_LingYong_Insert(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del, str, 手工具編號, 手工具名稱, 規格型號, 廠牌代號, 存放地點代號, 數量_2);
            frm.ShowDialog();
            Load_手工具台帳();
        }

        private void bt_維修_Click(object sender, EventArgs e)
        {
            str = "維修";
            ManualTool_WeiXiu_Insert frm = new ManualTool_WeiXiu_Insert(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del, str, 手工具編號, 手工具名稱, 規格型號, 廠牌代號, 存放地點代號, 數量_2);
            frm.ShowDialog();
            Load_手工具台帳();
        }

        private void bt_送檢_Click(object sender, EventArgs e)
        {
            str = "送檢";
            ManualTool_WeiXiu_Insert frm = new ManualTool_WeiXiu_Insert(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del, str, 手工具編號, 手工具名稱, 規格型號, 廠牌代號, 存放地點代號, 數量_2);
            frm.ShowDialog();
            Load_手工具台帳();
        }

        private void bt_報廢_Click(object sender, EventArgs e)
        {
            str = "報廢";
            ManualTool_WeiXiu_Insert frm = new ManualTool_WeiXiu_Insert(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del, str, 手工具編號, 手工具名稱, 規格型號, 廠牌代號, 存放地點代號, 數量_2);
            frm.ShowDialog();
            Load_手工具台帳();
        }

        private void bt_借用_Click(object sender, EventArgs e)
        {
            str = "借用";
            ManualTool_LingYong_Insert frm = new ManualTool_LingYong_Insert(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del, str, 手工具編號, 手工具名稱, 規格型號, 廠牌代號, 存放地點代號, 數量_2);
            frm.ShowDialog();
            Load_手工具台帳();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox11_Enter(object sender, EventArgs e)
        {

        }

        private void bt_匯出_Click(object sender, EventArgs e)
        {
            Export_Excel excel = new Export_Excel();
            DataTable dt = (DataTable)dataGrid_手工具.DataSource;
            excel.Export(dt, "Sheet1", "手工具" + cb_狀態.SelectedItem.ToString());
        }

    }
}
